﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PBKVideo;
using System.Diagnostics;

namespace PBKApplication
{
    public partial class Form1 : Form
    {
        private SearchResult res;
        private Button[] btnPages;
        private int curPage = 1;

        private delegate void ProcessSearchBarFinishedCallback(object sender, SearchFinishedEventArgs e);

        private Video selectedVideo;

        private void changeStatusLabel(string status, Bitmap icon = null)
        {
            statusLabel.Text = status;
            statusLabel.Image = icon;
        }

        public Form1()
        {
            InitializeComponent();

            btnPages = new Button[7];
            int pos = 68;
            for (int i = 0; i < 7; i++)
            {
                btnPages[i] = new Button();
                btnPages[i].Font = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                btnPages[i].Size = new System.Drawing.Size(33, 33);
                pos = pos + 5 + 33;
                btnPages[i].Location = new System.Drawing.Point(pos, 616);
                btnPages[i].Text = (i + 1).ToString();
                btnPages[i].UseVisualStyleBackColor = true;
                //btnPages[i].Enabled = false;
                btnPages[i].Click += new EventHandler(btnPages_Click);
            }
            //btnNext.Enabled = false;
            btnPrev.Enabled = false;
            btnNext.Click += new EventHandler(btnPages_Click);
            btnPrev.Click += new EventHandler(btnPages_Click);
            btnNext.Location = new Point(btnPages[6].Location.X + btnPages[6].Size.Width + 10, btnPages[6].Location.Y + 5);
            btnPrev.Location = new Point(btnPages[0].Location.X - btnNext.Size.Width - 10, btnPages[0].Location.Y + 5);
            this.Controls.AddRange(btnPages);

            changeStatusLabel(@"Ready.");
        }

        private void btnPages_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            if (b.Equals(btnPrev)) //<< Previous Next >>
            {
                if (curPage > 1)
                {
                    curPage--;
                }
            }
            else
                if (b.Equals(btnNext))
                {
                    curPage++;
                }
                else
                {
                    curPage = Convert.ToInt32(b.Text);
                }
            searchBar1.GetSearchResultOnPage(curPage);
        }

        private void searchBar1_SearchFinished(object sender, SearchFinishedEventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new ProcessSearchBarFinishedCallback(searchBar1_SearchFinished), sender, e);
            }
            else
            {
                //do whatever you want with the search result (e.SearchResult)
                if (e.SearchResult != null)
                {
                    System.IO.File.WriteAllText(@"D:\pbksayang.txt", e.SearchResult.RawJson);
                }

                btnNext.Enabled = true;
                btnPrev.Enabled = true;
                for (int i = 0; i < 7; i++)
                {
                    btnPages[i].Enabled = true;
                }

                if (e.SearchStat != SearchBar.SearchStats.OK)
                {
                    statusLabel.Text = @"Error: " + e.SearchStat;
                    statusLabel.Image = global::PBKApplication.Properties.Resources.caution;
                }
                else
                {
                    statusLabel.Text = @"OK";
                    statusLabel.Image = global::PBKApplication.Properties.Resources.tick;
                    foreach (Control c in panelVideoResult.Controls)
                    {
                        c.Dispose();
                    }
                    panelVideoResult.Controls.Clear();

                    curPage = e.SearchResult.CurrentPage;
                    if (curPage == 1)
                    {
                        btnPrev.Enabled = false;
                    }
                    else
                    {
                        btnPrev.Enabled = true;
                    }

                    int kiri, kanan;
                    kiri = curPage - 3;
                    kanan = curPage + 3;
                    if (kiri <= 0)
                    {
                        kiri = 1;
                        kanan = 7;
                    }
                    int cc = 0;
                    for (int i = kiri; i <= kanan; i++)
                    {
                        if (i == curPage)
                        {
                            btnPages[cc].BackColor = SystemColors.ActiveCaption;
                        }
                        else
                        {
                            btnPages[cc].BackColor = SystemColors.Control;
                        }
                        btnPages[cc++].Text = i.ToString();
                    }

                    res = e.SearchResult;
                    lblTotal.Text = res.TotalVideos.ToString();

                    List<PanelVideo> listPanelVideo = new List<PanelVideo>();
                    res = e.SearchResult;

                    int xx = 5;
                    int yy = 0;
                    foreach (Video video in res.Videos)
                    {
                        PanelVideo pv = new PanelVideo(video);
                        if (yy == 0)
                        {
                            yy = 5;
                        }
                        else
                        {
                            yy = yy + pv.Size.Height + 5;
                        }
                        pv.LocationPanel = new Point(xx, yy);
                        ContextMenuVideo cmv = new ContextMenuVideo(pv.VideoInfo);
                        cmv.downloadToolStripMenuItem.Click += new EventHandler(downloadToolStripMenuItem_Click);
                        cmv.playToolStripMenuItem.Click += new EventHandler(playToolStripMenuItem_Click);
                        pv.ContextMenuStrip = cmv;


                        listPanelVideo.Add(pv);
                    }

                    panelVideoResult.Controls.AddRange(listPanelVideo.ToArray());
                    this.Refresh();
                }
            }   
        }

        private void searchBar1_SearchStarted(object sender, EventArgs e)
        {
            statusLabel.Text = @"Processing...";
            statusLabel.Image = global::PBKApplication.Properties.Resources.ajax_loader;
            for (int i = 0; i < 7; i++)
            {
                btnPages[i].Enabled = false;
            }
            btnPrev.Enabled = btnNext.Enabled = false;
            this.Refresh();
        }

        private void searchBar1_ExtractVideoURLCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            changeStatusLabel(@"Extracting video urls completed...", global::PBKApplication.Properties.Resources.tick);
            List<VideoQuality> ls = e.Result as List<VideoQuality>;
            searchBar1.ExtractVideoURLCompleted -= searchBar1_ExtractVideoURLCompleted;
            using (FormDownload fd = new FormDownload(ls, selectedVideo, searchBar1))
            {
                fd.ShowDialog(this);
            }
            //foreach (VideoQuality v in ls)
            //{
            //    textBox1.AppendText(v.SizeInMB + " " + v.Extension + " " + v.DownloadURL + Environment.NewLine + Environment.NewLine);
            //}
        }

        private void playToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            ContextMenuVideo menu = item.GetCurrentParent() as ContextMenuVideo;
            selectedVideo = menu.VideoInfo;
            using (PlayForm pf = new PlayForm(selectedVideo))
            {
                pf.ShowDialog(this);
            }
        }

        private void downloadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            ContextMenuVideo menu = item.GetCurrentParent() as ContextMenuVideo;
            selectedVideo = menu.VideoInfo;
            changeStatusLabel(@"Extracting video urls...", global::PBKApplication.Properties.Resources.ajax_loader);
            searchBar1.ExtractVideoURLs(menu.VideoInfo);
        }
    }
}
